package 多线程;

/*
    练习:
        银行有一个账户,有两个储户分别向同一个账户存3000元,每次存1000,存3次,每次存完打印账户余额

 */
public class ThreadTest12 {
    public static void main(String[] args) {
        Account acct = new Account(0.0);
        Customer c1 = new Customer(acct);
        Customer c2 = new Customer(acct);

        c1.setName("甲");
        c2.setName("乙");

        c1.start();
        c2.start();
    }
}

class Account{

    private Double balance;

    public Account(Double balance) {
        this.balance = balance;
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    //存钱
    public synchronized void deposit(Double amt){
        if(amt != null && amt.doubleValue() > 0){
            this.balance += amt;

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + ",余额为:" + this.balance);
        }
    }
}

class Customer extends Thread{

    private Account acct;

    public Customer(Account acct){
        this.acct = acct;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            acct.deposit(new Double(1000));
        }
    }
}
